<!doctype html>
<html lang="en">
<head>
    @include('web.layout.head')
    <link rel="stylesheet" href="{{asset('/css/web/user/address.css')}}">
    <script src="{{asset('/js/web/user/address.js')}}"></script>
</head>
<body>
@include('web.layout.header')
@include('web.layout.user_tab')
<div class="base_member_content">
    {{csrf_field()}}
    <div class="w1200">
        @include('web.layout.member_nav')
        <div class="nav_right flr">
            <div class="base_content">
                <div class="line_title">收货地址</div>
                <div class="address-data">
                    <div class="tit fl">已保存收货地址</div>
                    @if(count($list) < 10)
                        <a href="{{url('/member/address/create')}}" id="address_add">+新增收货地址</a>
                    @endif
                </div>
                <table border="0" cellspacing="0" cellpadding="0" class="address-tbl">
                    <thead>
                    <tr>
                        <th class="col-name">收货人</th>
                        <th class="col-phone">联系电话</th>
                        <th class="col-address">收货地址</th>
                        <th class="colw1">操作</th>
                        <th class="colw2">备注</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach($list as $item)
                        <tr>
                            <td class="col-name">{{$item['receiver']}}</td>
                            <td class="col-phone">{{$item['phone']}}</td>
                            <td class="col-address">{{$item['address']}}</td>
                            <td class="colw1">{{$item['is_default'] ? '默认地址' : ''}}</td>
                            <td class="colw2">
                                @if(!$item['is_default'])
                                    <a href="javascript:;" data-id="{{$item['id']}}" class="js-default">设为默认</a>
                                @endif
                                <a href="{{url('/member/address/'.$item['id'].'/edit')}}">编辑</a>
                                <a href="javascript:;" data-id="{{$item['id']}}" class="js-remove">删除</a>
                            </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
@include('web.layout.footer')
<script>
    $(function () {
        $('.js-remove').click(function () {
            var $this = $(this), aid = $this.data('id');
            var token = $('input[name="_token"]').val();
            layer.confirm('确认要删除这条地址记录吗？', {
                btn: ['确定', '取消'] //可以无限个按钮
            }, function (index) {
                $.ajax({
                    url: '/member/address/' + aid,
                    type: 'DELETE',
                    dataType: 'json',
                    data:{_token:token},
                    success: function (data) {
                        if (data.code == 0) {
                            layer.alert('删除成功', {
                                icon: 1
                            }, function () {
                                location.reload();
                            });
                        } else {
                            layer.alert('删除失败', {
                                icon: 0
                            });
                        }
                    }
                });
            });
        })
        $(".js-default").click(function (event) {
            /* Act on the event */
            var address_id = $(this).attr('data-id');
            var token = $('input[name="_token"]').val();
            $.ajax({
                url: '/member/address/' + address_id + '/set_default',
                type: 'PUT',
                dataType: 'json',
                data:{_token:token},
                success: function (data) {
                    if (0 == data.code) {
                        layer.alert('设置成功！', {
                            icon: 1
                        }, function () {
                            location.reload();
                        });
                    } else {
                        layer.alert(data.message, {
                            icon: 1
                        });
                    }
                }
            });

        });
    })
</script>
</body>
</html>